<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      title="文章详情"
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->

    <!-- 加载中 loading -->
    <van-loading v-if="loading" class="article-loading" />
    <!-- /加载中 loading -->

    <!-- 文章详情 -->
    <div class="detail">
      <h3 class="title">{{ articleInfo.title }}</h3>
      <div class="author">
        <van-image round width="1rem" height="1rem" fit="fill" :src="articleInfo.aut_photo" />
        <div class="text">
          <p class="name">{{ articleInfo.aut_name }}</p>
          <p class="time">{{ articleInfo.pubdate | relativeTime }}</p>
        </div>
        <van-button
          round
          size="small"
          :type="articleInfo.is_followed?'danger':'info'"
          @click="followBtn(articleInfo.aut_id.toString())"
        >{{ articleInfo.is_followed?'取消关注':'+ 关注' }}</van-button>
      </div>
      <div class="content">
        <div v-html="articleInfo.content" />
      </div>
      <van-divider>END</van-divider>
      <div class="zan">
        <!-- 点赞 -->
        <van-button
          round
          size="small"
          hairline
          type="primary"
          plain
          icon="good-job-o"
          @click="likings(articleInfo.art_id.toString())"
        >{{ articleInfo.attitude===1?'取消点赞': '点赞' }}</van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <!-- 不喜欢 -->
        <van-button
          round
          size="small"
          hairline
          type="danger"
          plain
          icon="delete"
          @click="dislike(articleInfo.art_id.toString())"
        >{{ articleInfo.attitude===0?'取消不喜欢': '不喜欢' }}</van-button>
      </div>
    </div>
    <!-- /文章详情 -->

    <!-- 文章评论 -->
    <article-comment
      v-if="articleInfo.art_id"
      :articleid="articleInfo.art_id.toString()"
      @commentReply="handleCommentReply"
    />
    <!-- 评论回复 -->
    <van-popup
      v-model="showReply"
      round
      position="bottom"
      style="height:80%"
    >
      <comment-reply
        v-if="showReply"
        :info="commentInfo"
        :artid="this.$route.params.id.toString()"
        @offReply="showReply=false"
      />
    </van-popup>
  </div>
</template>

<script>
// 接口引入
import { getArtical, disLike, unDislike, likings, unLikings } from '@/api/articles'
import { followUser, unFollowUser } from '@/api/user'

// 组件注册
import ArticleComment from '@/views/Article/componts/ArticleComment'
import CommentReply from '@/views/Article/componts/CommentReply'
export default {
  name: 'Articel',
  components: { ArticleComment, CommentReply },
  data() {
    return {
      articleInfo: {},
      loading: true,
      showReply: false,
      commentInfo: null
    }
  },
  async created() {
    try {
      // 获取文章详情
      const res = await getArtical(this.$route.params.id.toString())
      // 同步文章详情
      this.articleInfo = res.data
      // 调整loading为false
      this.loading = false
    } catch (error) {
      this.$toast.fail('获取详情失败')
    }
  },
  methods: {
    // 点击 关注/取关 按钮
    async followBtn(id) {
      if (this.articleInfo.is_followed) {
        try {
          await unFollowUser(id)
          this.$toast.success('取消关注')
          this.articleInfo.is_followed = !this.articleInfo.is_followed
        } catch (error) {
          this.$toast.fail('操作失败')
        }
      } else {
        try {
          await followUser(id)
          this.$toast.success('关注成功')
          this.articleInfo.is_followed = !this.articleInfo.is_followed
        } catch (error) {
          this.$toast.fail('操作失败')
        }
      }
    },

    // 点击 点赞/取消点赞 按钮
    async likings(id) {
      if (this.articleInfo.attitude === 1) {
        try {
          await unLikings(id)
          this.$toast.success('取消点赞成功')
          this.articleInfo.attitude = -1
        } catch (error) {
          this.$toast.fail('操作失败')
        }
      } else {
        try {
          await likings(id)
          this.articleInfo.attitude = 1
          this.$toast.success('点赞成功')
        } catch (error) {
          this.$toast.fail('操作失败')
        }
      }
    },

    // 点击 不喜欢/取消不喜欢 按钮
    async dislike(id) {
      if (this.articleInfo.attitude === 0) {
        try {
          await unDislike(id)
          this.$toast.success('取消不喜欢')
          this.articleInfo.attitude = -1
        } catch (error) {
          this.$toast.fail('操作失败')
        }
      } else {
        try {
          await disLike(id)
          this.articleInfo.attitude = 0
          this.$toast.success('不喜欢')
        } catch (error) {
          this.$toast.fail('操作失败')
        }
      }
    },

    // 文章评论组件触发回复按钮
    handleCommentReply(item) {
      this.commentInfo = item
      this.showReply = true
    }
  }
}
</script>

<style lang='scss' scoped>
.article-container{
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error{
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan{
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size:14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    ::v-deep img{
      max-width:100%;
      background: #f9f9f9;
    }
  }
}
</style>
